using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
namespace com.dtn.deals
{
	public class tbl_CommentManager : Itbl_CommentManager
	{
		private static Itbl_CommentManager itbl_CommentManager = null;
		private tbl_CommentManager()
		{
		}
		public static Itbl_CommentManager CreateInstance()
		{
			if (itbl_CommentManager == null)
			{
				itbl_CommentManager = new tbl_CommentManager();
			}
			return itbl_CommentManager;
		}
		public int Inserttbl_Comment(tbl_Comment tbl_comment)
		{
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@proId", SqlDbType.Int),
																new SqlParameter("@partnerId", SqlDbType.Int),
																new SqlParameter("@title", SqlDbType.NVarChar),
																new SqlParameter("@fullName", SqlDbType.NVarChar),
																new SqlParameter("@userId", SqlDbType.Int),
																new SqlParameter("@content", SqlDbType.NVarChar),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = tbl_comment.proId;
			parameters[2].Value = tbl_comment.partnerId;
			parameters[3].Value = tbl_comment.title;
			parameters[4].Value = tbl_comment.fullName;
			parameters[5].Value = tbl_comment.userId;
			parameters[6].Value = tbl_comment.content;
			parameters[7].Value = tbl_comment.cDate;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_Comment", CommandType.StoredProcedure, parameters);
				tbl_comment.ID = Convert.ToInt32(parameters[0].Value);
			}
			catch (Exception ex)
			{
				tbl_comment.ID = -1;
			}
			return tbl_comment.ID;
		}
		public bool Updatetbl_Comment(tbl_Comment tbl_comment)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@proId", SqlDbType.Int),
																new SqlParameter("@partnerId", SqlDbType.Int),
																new SqlParameter("@title", SqlDbType.NVarChar),
																new SqlParameter("@fullName", SqlDbType.NVarChar),
																new SqlParameter("@userId", SqlDbType.Int),
																new SqlParameter("@content", SqlDbType.NVarChar),
																new SqlParameter("@cDate", SqlDbType.DateTime),
                                                                new SqlParameter("@IsCensorship", SqlDbType.TinyInt),
															};
			parameters[0].Value = tbl_comment.ID;
			parameters[1].Value = tbl_comment.proId;
			parameters[2].Value = tbl_comment.partnerId;
			parameters[3].Value = tbl_comment.title;
			parameters[4].Value = tbl_comment.fullName;
			parameters[5].Value = tbl_comment.userId;
			parameters[6].Value = tbl_comment.content;
			parameters[7].Value = tbl_comment.cDate;
            parameters[8].Value = tbl_comment.IsCensorship;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_Comment", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public bool Deletetbl_Comment(int id)
		{
			bool bresult = false;
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			int inumOfRowEffect = 0;
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_Comment", CommandType.StoredProcedure, parameters);
				bresult = true;
			}
			catch (Exception ex)
			{
				bresult = false;
			}
			return bresult;
		}
		public tbl_Comment Selecttbl_CommentByID(int id)
		{
			IDataReader dr;
			tbl_Comment tbl_comment = new tbl_Comment();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
			parameters[0].Value = id;
			try
			{
				dr = dataProvider.ExecuteReader("sp_Selecttbl_Comment", CommandType.StoredProcedure, parameters);
				while(dr.Read())
				{
					tbl_comment.ID = Convert.ToInt32(dr["ID"].ToString());
					tbl_comment.proId = Convert.ToInt32(dr["proId"].ToString());
					tbl_comment.partnerId = Convert.ToInt32(dr["partnerId"].ToString());
					tbl_comment.title = (dr["title"].ToString());
					tbl_comment.fullName = (dr["fullName"].ToString());
					tbl_comment.userId = Convert.ToInt32(dr["userId"].ToString());
					tbl_comment.content = (dr["content"].ToString());
					tbl_comment.cDate = Convert.ToDateTime(dr["cDate"].ToString());
                    tbl_comment.IsCensorship = Convert.ToInt32(dr["isCensorship"].ToString());
				}

			}
			catch (Exception ex)
			{
				return null;
			}
			return tbl_comment;
		}
		public DataTable Selecttbl_CommentsDynamic(string Where, string Order)
		{
			DataSet ds = new DataSet();
			DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
			SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
			parameters[0].Value = Where;
			parameters[1].Value = Order;
			try
			{
				ds = dataProvider.ExecuteDataSet("sp_Selecttbl_CommentsDynamic", CommandType.StoredProcedure, parameters);
			}
			catch (Exception ex)
			{
				return null;
			}
			return ds.Tables["Table"];
		}

        public DataTable Selecttbl_CommentsWithPages(int StartIndex, int MaxRecords, string Where) 
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parrams = { new SqlParameter("@Startindex",SqlDbType.Int ){Value = StartIndex },
                                         new SqlParameter("@Maxrecords",SqlDbType.Int ){Value = MaxRecords  },
                                         new SqlParameter("@WhereCondition",SqlDbType.NVarChar,500 ){Value = Where  }
                                     };
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_CommentWithPagesDynamic", CommandType.StoredProcedure, parrams);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }
	}
}
